package com.smartscreen.contentmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.smartscreen.contentmanage.entity.ClassMoment;
import com.smartscreen.contentmanage.mapper.ClassMomentMapper;
import com.smartscreen.contentmanage.service.ClassMomentService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClassMomentServiceImpl extends ServiceImpl<ClassMomentMapper, ClassMoment> implements ClassMomentService {
    
    @Override
    public List<ClassMoment> getMomentsByClassId(String classId) {
        QueryWrapper<ClassMoment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id", classId)
                   .eq("status", "approved")
                   .orderByDesc("created_at");
        return this.list(queryWrapper);
    }

    @Override
    public boolean approveMoment(String momentId, String status, String reason) {
        UpdateWrapper<ClassMoment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("moment_id", momentId)
                .set("status", status)
                .set("updated_at", LocalDateTime.now());

        // 如果是拒绝状态且有原因，可以添加拒绝原因字段
        if ("rejected".equals(status) && StringUtils.hasText(reason)) {
            updateWrapper.set("reject_reason", reason);
        }

        return this.update(updateWrapper);
    }
}